package com.googlecode.jtoy.patterns.command;

public class SleepCommand implements Command {

	private Command command = null;
	private ActiveObjectEngine engine = null;
	private long sleepTime = 0;
	private long startTime = 0;
	private boolean started = false;

	public SleepCommand(long sleepTime, ActiveObjectEngine engine, Command command) {

		this.sleepTime = sleepTime;
		this.engine = engine;
		this.command = command;
	}

	@Override
	public void execute() throws Exception {

		long currentTime = System.currentTimeMillis();
		if (!started) {
			started = true;
			startTime = currentTime;
			engine.addCommand(this);
		}
		else if ((currentTime - startTime) < sleepTime) {
			engine.addCommand(this);
		}
		else {
			engine.addCommand(command);
		}
	}
}
